package com.example.imooc_utils.lambda.service;

import com.example.imooc_utils.lambda.pojo.Sku;
import com.example.imooc_utils.utils.SkuCategoryEnum;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.BinaryOperator;
import java.util.function.Predicate;

/**
 * @author lxp
 * @program imooc_utils
 * @description 购物车服务类
 * @create: 2019-12-23 17:33
 */
public class SkuService {

	/**
	 * 加入到购物车的商品
	 */
	private static List<Sku> cartSkuList = new ArrayList<Sku>() {

		{
			add(new Sku(654032, "无人机", 4999.00, 1, 4999.00, SkuCategoryEnum.ELECTRONICS));
			add(new Sku(642934, "vr-一体机", 2999.00, 1, 2999.00, SkuCategoryEnum.ELECTRONICS));
			add(new Sku(645321, "纯色寸衫", 409.00, 3, 1227.00, SkuCategoryEnum.CLOTHING));
			add(new Sku(654327, "牛仔裤", 528.00, 1, 528.00, SkuCategoryEnum.CLOTHING));
			add(new Sku(675489, "跑步机", 2699.00, 1, 2699.00, SkuCategoryEnum.SPORTS));
			add(new Sku(644564, "java编程思想", 79.00, 1, 79.00, SkuCategoryEnum.BOOKS));
			add(new Sku(678678, "java核心技术", 149.00, 1, 149.00, SkuCategoryEnum.BOOKS));
			add(new Sku(697894, "算法", 78.20, 1, 78.20, SkuCategoryEnum.BOOKS));
			add(new Sku(696968, "进阶指南", 85.10, 1, 85.00, SkuCategoryEnum.BOOKS));
		}
	};


	public static List<Sku> getCartSkuList() {

		return cartSkuList;
	}

	/**
	 * version 1.0.0
	 * 需求1 找出电子产品
	 *
	 * @param cartSkuList
	 * @return
	 */
	//
	public static List<Sku> filterElectronicsSkus(List<Sku> cartSkuList) {

		List<Sku> result = new ArrayList<Sku>();

		for (Sku sku : cartSkuList) {
			if (SkuCategoryEnum.ELECTRONICS.equals(sku.getSkuCategory())) {
				result.add(sku);
			}
		}

		return result;
	}


	/**
	 * version 2.0.0
	 * 需求2 根据传入的商品类型 查找对应的商品信息
	 *
	 * @param cartSkuList
	 * @return
	 */
	//
	public static List<Sku> filterByCategorySkus(List<Sku> cartSkuList, SkuCategoryEnum category) {

		List<Sku> result = new ArrayList<Sku>();

		for (Sku sku : cartSkuList) {
			if (category.equals(sku.getSkuCategory())) {
				result.add(sku);
			}
		}

		return result;
	}


	/**
	 * version 3.0.0
	 * 需求3 满足是根据商品价格或商品类型来过滤商品信息
	 *
	 * @param cartSkuList
	 * @return
	 */
	//
	public static List<Sku> filterByCategorySkus(List<Sku> cartSkuList, SkuCategoryEnum category, Double totalPrice, Boolean categoryorprice) {

		List<Sku> result = new ArrayList<Sku>();
		for (Sku sku : cartSkuList) {
			if ((categoryorprice && category.equals(sku.getSkuCategory())) || (!categoryorprice && sku.getSkuPrice() > totalPrice)) {
				result.add(sku);
			}
		}
		return result;
	}


	/**
	 * version 4.00
	 * 根据不同的sku判断标准 ，对sku列表进行过滤   === 设计模式中的处理模式
	 *
	 * @param cartSkuList
	 * @param skuPredicate - 根据不同的实现策略来实现不同的判断标准
	 * @return
	 */
	public static List<Sku> filterSku(List<Sku> cartSkuList, SkuPredicate skuPredicate) {

//        BinaryOperator<Long> fun  = (x,y ) -> x+y;

		List<Sku> result = new ArrayList<Sku>();
		for (Sku sku : cartSkuList) {
			if (skuPredicate.test(sku)) {
				result.add(sku);
			}
		}

		return result;

	}


}
